package com.study.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.study.common.constant.ChuangLanHomeSmsConstants;
import com.study.common.constant.ChuangLanInternationalSmsConstants;
import com.study.common.enums.SmsTemplateDataEnum;
import com.study.common.model.ChuangLanSmsResult;
import com.study.common.properties.ChuangLanHomeProperties;
import com.study.common.properties.ChuangLanInternationalProperties;
import com.study.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
//import sun.net.www.http.HttpClient;

import java.util.*;

/**
 * @author nizhihao
 * @version 1.0.0
 * @date 2023/12/4 10:39
 */
@Slf4j
public class ChuangLanSmsUtils {

    private static final ChuangLanInternationalProperties chuangLanInternationalProperties = SpringUtils.getBean(ChuangLanInternationalProperties.class);
    private static final ChuangLanHomeProperties chuangLanHomeProperties = SpringUtils.getBean(ChuangLanHomeProperties.class);
    private static final RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class);

    private static final List<String> HOME_CODE = Arrays.asList("86");

    public static ChuangLanSmsResult send(String areaCode, SmsTemplateDataEnum smsTemplateData, String mobile, String msg) {
        if (HOME_CODE.contains(areaCode)) {
            return sendHome(smsTemplateData.getZhCn(), mobile, msg);
        } else {
            return internationalSend(areaCode, smsTemplateData.getUsEn(), mobile, msg);
        }
    }

    public static ChuangLanSmsResult sendNotify(String areaCode, SmsTemplateDataEnum smsTemplateData, String mobile, String msg) {
        if (HOME_CODE.contains(areaCode)) {
            return sendHomeNotify(smsTemplateData.getZhCn(), mobile, msg);
        } else {
            return internationalSendNotify(areaCode, smsTemplateData.getUsEn(), mobile, msg);
        }
    }

    public static ChuangLanSmsResult sendHome(String templateData, String mobile, String msg) {

        HashMap<String, String> map = new HashMap<>();
        map.put(ChuangLanHomeSmsConstants.ACCOUNT, chuangLanHomeProperties.getReceiver());
        map.put(ChuangLanHomeSmsConstants.PASSWORD, chuangLanHomeProperties.getPswd());

        msg = templateData.replace(ChuangLanHomeSmsConstants.VARIABLE, msg);
        map.put(ChuangLanHomeSmsConstants.MSG, msg);
        map.put(ChuangLanHomeSmsConstants.PHONE, mobile);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=utf-8;");
        headers.setContentType(MediaType.APPLICATION_JSON);
        System.out.println(chuangLanHomeProperties.getUrl());
        HttpEntity<HashMap<String, String>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<ChuangLanSmsResult> forEntity =
                restTemplate.exchange(chuangLanHomeProperties.getUrl(), HttpMethod.POST, requestEntity, ChuangLanSmsResult.class);

        log.info("【短信国内】: phone:{}, msg:{}, response:{}", mobile, msg, forEntity);

        return forEntity.getBody();
    }
    public static ChuangLanSmsResult sendHomeNotify(String templateData, String mobile, String msg) {

        HashMap<String, String> map = new HashMap<>();
        map.put(ChuangLanHomeSmsConstants.ACCOUNT, chuangLanHomeProperties.getNotifyreceiver());
        map.put(ChuangLanHomeSmsConstants.PASSWORD, chuangLanHomeProperties.getNotifypwd());

        msg = templateData.replace(ChuangLanHomeSmsConstants.VARIABLE, msg);
        map.put(ChuangLanHomeSmsConstants.MSG, msg);
        map.put(ChuangLanHomeSmsConstants.PHONE, mobile);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=utf-8;");
        headers.setContentType(MediaType.APPLICATION_JSON);
        System.out.println(chuangLanHomeProperties.getUrl());
        HttpEntity<HashMap<String, String>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<ChuangLanSmsResult> forEntity =
                restTemplate.exchange(chuangLanHomeProperties.getUrl(), HttpMethod.POST, requestEntity, ChuangLanSmsResult.class);

        log.info("【短信国内】: phone:{}, msg:{}, response:{}", mobile, msg, forEntity);

        return forEntity.getBody();
    }
    public static ChuangLanSmsResult internationalSend(String areaCode, String templateData, String mobile, String msg) {
        mobile = areaCode + mobile;
        String time = String.valueOf(System.currentTimeMillis());
        TreeMap<String, String> paramMap = new TreeMap<>();
        paramMap.put(ChuangLanInternationalSmsConstants.NONCE, time);
        paramMap.put(ChuangLanInternationalSmsConstants.ACCOUNT, chuangLanInternationalProperties.getReceiver());
        paramMap.put(ChuangLanInternationalSmsConstants.MOBILE, mobile);
        msg = templateData.replace(ChuangLanInternationalSmsConstants.VARIABLE, msg);
        paramMap.put(ChuangLanInternationalSmsConstants.MSG, msg);

        String sign = ChuangLanSignUtils.sendSign(chuangLanInternationalProperties.getPswd(), paramMap);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=utf-8;");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(ChuangLanInternationalSmsConstants.NONCE, time);
        headers.add(ChuangLanInternationalSmsConstants.SIGN, sign);

        HttpEntity<TreeMap<String, String>> requestEntity = new HttpEntity<>(paramMap, headers);
        ResponseEntity<ChuangLanSmsResult> forEntity =
                restTemplate.exchange(chuangLanInternationalProperties.getUrl(), HttpMethod.POST, requestEntity, ChuangLanSmsResult.class);

        log.info("【短信国际】: phone:{}, msg:{}, response:{}", mobile, msg, forEntity);

        return forEntity.getBody();
    }
    public static ChuangLanSmsResult internationalSendNotify(String areaCode, String templateData, String mobile, String msg) {
        mobile = areaCode + mobile;
        String time = String.valueOf(System.currentTimeMillis());
        TreeMap<String, String> paramMap = new TreeMap<>();
        paramMap.put(ChuangLanInternationalSmsConstants.NONCE, time);
        paramMap.put(ChuangLanInternationalSmsConstants.ACCOUNT, chuangLanInternationalProperties.getReceiver());
        paramMap.put(ChuangLanInternationalSmsConstants.MOBILE, mobile);
        msg = templateData.replace(ChuangLanInternationalSmsConstants.VARIABLE, msg);
        paramMap.put(ChuangLanInternationalSmsConstants.MSG, msg);

        String sign = ChuangLanSignUtils.sendSign(chuangLanInternationalProperties.getPswd(), paramMap);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=utf-8;");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(ChuangLanInternationalSmsConstants.NONCE, time);
        headers.add(ChuangLanInternationalSmsConstants.SIGN, sign);

        HttpEntity<TreeMap<String, String>> requestEntity = new HttpEntity<>(paramMap, headers);
        ResponseEntity<ChuangLanSmsResult> forEntity =
                restTemplate.exchange(chuangLanInternationalProperties.getUrl(), HttpMethod.POST, requestEntity, ChuangLanSmsResult.class);

        log.info("【短信国际】: phone:{}, msg:{}, response:{}", mobile, msg, forEntity);

        return forEntity.getBody();
    }

}
